package home

import (
	"fmt"
	"gitee.com/jikey/elk-blog/app/model"
	"gitee.com/jikey/elk-blog/app/service"
	"gitee.com/jikey/elk-blog/pkg/response"
	"gitee.com/jikey/elk-blog/pkg/utils"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"html/template"
	"strconv"
)

type Blog struct {
	Controller
}

func init() {
	blog := &Blog{}
	_ = blog.Init(blog)
	blog.Menu()

	Elk = make(map[interface{}]interface{})
	Elk["Menu"] = blog.Data["Menu"]
	Elk["BlogCategory"] = blog.Data["BlogCategory"]
	Elk["ArchiveList"] = blog.Data["ArchiveList"]
}

// Index 首页
func (b *Blog) Index(c *gin.Context) {
	data := make(map[string]interface{})
	pageNum, _ := strconv.Atoi(c.Query("page"))

	keywords := c.Query("keywords")
	maps := "(tk_blog.status = 1) AND (title like '%" + keywords + "%')"

	// 获取分页数据
	page := utils.NewPagination(c.Request, service.Blog.GetBlogTotal(maps), utils.PageSize)
	data["page"] = template.HTML(page.Pages())

	data["menu"] = Elk["Menu"]
	data["blogCategory"] = Elk["BlogCategory"]
	data["archiveList"] = service.Blog.BlogArchiveList()
	data["list"] = service.Blog.BlogList(pageNum, utils.PageSize, maps)
	data["link"] = service.Link.List(1, 100, "")
	data["site"] = Site
	data["active"] = "blog"

	response.Render(c, "blog/index", data)
}

// Detail 详情
func (b *Blog) Detail(c *gin.Context) {
	data := make(map[string]interface{})

	id, _ := strconv.Atoi(c.Param("id"))
	pageNum, _ := strconv.Atoi(c.Query("page"))

	maps := "bid=" + c.Param("id")

	detail, _ := service.Blog.BlogDetail(id)
	data["blog"] = detail
	data["blogCategory"] = Elk["BlogCategory"]
	data["archiveList"] = service.Blog.BlogArchiveList()

	data["commentList"] = service.Comment.List(pageNum, utils.PageSize, maps)
	data["commentTotal"] = service.Comment.Total(maps)
	data["prev"] = service.Blog.GetPrevNextBlog(detail.Createtime, "prev")
	data["next"] = service.Blog.GetPrevNextBlog(detail.Createtime, "next")

	data["link"] = service.Link.List(1, 100, "")
	data["site"] = Site
	data["active"] = "blog"

	response.Render(c, "blog/detail", data)
}

// Archive 归档
func (b *Blog) Archive(c *gin.Context) {
	data := make(map[string]interface{})

	pageNum, _ := strconv.Atoi(c.Query("page"))
	total, _ := strconv.Atoi(c.Query("total"))
	id := c.Param("id")

	// 获取分页数据
	page := utils.NewPagination(c.Request, total, utils.PageSize)

	data["page"] = template.HTML(page.Pages())
	data["blogCategory"] = Elk["BlogCategory"]
	data["archiveList"] = service.Blog.BlogArchiveList()
	data["list"] = service.Blog.GetArchive(pageNum, utils.PageSize, id)
	data["link"] = service.Link.List(1, 100, "")
	data["site"] = Site
	data["active"] = "blog"

	response.Render(c, "archive/index", data)
}

// Category 分类
func (b *Blog) Category(c *gin.Context) {
	data := make(map[string]interface{})
	maps := make(map[string]interface{})

	pageNum, _ := strconv.Atoi(c.Query("page"))

	id := c.Param("id")
	maps["catid"] = id

	// 获取分页数据
	page := utils.NewPagination(c.Request, service.Blog.GetBlogTotal(maps), utils.PageSize)

	data["page"] = template.HTML(page.Pages())
	data["blogCategory"] = Elk["BlogCategory"]
	data["archiveList"] = service.Blog.BlogArchiveList()
	data["list"] = service.Blog.BlogList(pageNum, utils.PageSize, maps)
	data["link"] = service.Link.List(1, 100, "")
	data["site"] = Site
	data["active"] = "blog"

	response.Render(c, "category/index", data)
}

// Search 搜索
func (b *Blog) Search(c *gin.Context) {
	data := make(map[string]interface{})
	maps := make(map[string]interface{})

	// 获取url 参数信息
	pageNum := 1
	if pageQuery := c.Query("page"); len(pageQuery) > 0 {
		var err error
		pageNum, err = strconv.Atoi(pageQuery)
		if err != nil {
			logrus.Error("分页参数转换有误")
			return
		}
	}

	keywords := c.Query("keywords")
	maps["title"] = keywords

	// 获取分页数据
	page := utils.NewPagination(c.Request, service.Blog.GetBlogTotal(maps), utils.PageSize)

	data["page"] = template.HTML(page.Pages())
	data["list"] = service.Blog.BlogList(pageNum, utils.PageSize, maps)
	data["site"] = Site
	data["active"] = "blog"

	response.Render(c, "blog/search", data)
}

// Likes 点赞
func (b *Blog) Likes(c *gin.Context) {
	id := c.Param("id")

	blog := &model.Blog{}

	if err := c.ShouldBind(&blog); err != nil {
		c.JSON(400, gin.H{
			"err": err.Error(),
		})
		return
	}
	fmt.Println("blog", blog.Likes)

	service.Blog.UpdateField(id, "likes", blog.Likes)

	response.Success(c, blog.Likes)
}
